<template>
	<view>
		<cu-custom bgColor="bg-blue" isBack>
			<block slot="content">打包工作</block>
		</cu-custom>
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">单号</view>
				<input placeholder="请扫码单号" name="input" ref="gain" v-model="inputValue" disabled></input>
				<!-- <input placeholder="请扫码单号" name="input" ref="gain" v-model="inputValue"></input> -->
			</view>
		</form>

		<view class="cu-bar bg-white solid-bottom margin-top">
			<view class="action">
				<text class="cuIcon-title text-orange"></text> 今日单号列表
			</view>
			<view class="action">
				<text class="cuIcon-title text-orange"></text> 今日单号总计 {{count}}
			</view>
		</view>
		<view class="cu-list menu margin-top sm-border">
			<view class="cu-item" v-for="(item,index) in lists">
				<view class="content">
					<text class="text-grey">单号：{{item.express}}</text>
					<!-- <text class="text-grey">日期：{{scanTime|shijianfilter("yyy-mm-dd hh:mm:ss")}}</text> -->
				</view>
				<view class="action" @tap="del" :data-id="index" :data-express="item.express">
					<button class="cu-btn round bg-red shadow">
						<text class="cuIcon-delete"></text>删除
					</button>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	var main, receiver, filter;
	var _codeQueryTag = false;
	export default {
		data() {
			return {
				inputValue: "",
				lists: {},
				count: 0,
				uuid: '',
				nowTime: new Date(new Date().setHours(0, 0, 0, 0)) / 1000 * 1000
			}
		},
		filters: {
			shijianfilter: function(value, args) {
				var dt = new Date(value)
				var y = dt.getFullYear()
				//这里month得加1
				var m = dt.getMonth() + 1
				var d = dt.getDate()
				//如果要求的时间格式只有年月日
				if (args.toLowerCase() === "yyy-mm-dd") {
					return `${y}-${m}-${d}`
					//如果时间要求精确到时分秒
				} else {
					var hh = dt.getHours()
					var mm = dt.getMinutes()
					var ss = dt.getSeconds();
					return `${y}-${m}-${d}:${hh}:${mm}:${ss}`
				}
			}
		},
		watch: {
			lists(val) {
				this.count = val.length;
			},
			inputValue(val) {
				console.log("变啦");
				console.log(val);
				
				this.shibai.play()
			}
		},
		methods: {
			initScan() {
				let _this = this;
				/* #ifdef APP-PLUS */
				main = plus.android.runtimeMainActivity(); //获取activity  
				/* var context = plus.android.importClass('android.content.Context'); */
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				/* var Intent = plus.android.importClass('android.content.Intent'); */
				filter = new IntentFilter();
				filter.addAction("android.intent.action.SCANRESULT");
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						/*这个se4500很坑，不同的手机或pda，这个值就不一样，要具体去查硬件api*/
						let code = intent.getStringExtra("value");
						_this.inputValue = code;
						_this.add(code)

						/* rmker.showTip('条码:'+code); */
						/*调用本页面某方法*/
						_this.queryCode(code);
					}
				});
				/* #endif */
			},
			startScan() {
				/* #ifdef APP-PLUS */
				main.registerReceiver(receiver, filter);
				/* #endif */
			},
			stopScan() {
				/* #ifdef APP-PLUS */
				main.unregisterReceiver(receiver);
				/* #endif */
			},
			queryCode: function(code) {
				//防重复  
				if (_codeQueryTag) return false;
				_codeQueryTag = true;
				setTimeout(function() {
					_codeQueryTag = false;
				}, 150);

				// console.log(code);
				//到这里扫描成功了，可以调用自己的业务逻辑，code就是扫描的结果  
			},
			add(code) {
				//本地SQLite存储
				plus.sqlite.selectSql({
					name: 'express',
					// sql: `select * from express where express = ${this.inputValue} and status = 1 and type = "unbox"`,
					sql: `select * from express where express = "${code}" and status = 1 and type = "box"`,
					success: (data) => {
						// console.log(JSON.stringify(data));
						// console.log(this.uuid);
						if (data.length > 0) {
							uni.showToast({
								title: "重复啦",
								icon: "none"
							})
							var s = plus.audio.createPlayer("./static/chognfu.mp3");
							s.play()

						} else {
							console.log("准备插入");
							plus.sqlite.executeSql({
								name: 'express',
								sql: `insert into express values("${code}",${Number(Date.now())},'box',"${this.uuid}",1)`,
								success: (e)=> {
									console.log('插入成功');
									// var s = plus.audio.createPlayer("./static/luru.mp3");
									var chenggong = plus.audio.createPlayer("./static/chenggong.mp3");
									chenggong.play();
								},
								fail: (e)=> {
									var shibai = plus.audio.createPlayer("./static/shibai.mp3");
									shibai.play();
									console.log('executeSql failed: ' + JSON.stringify(e));
								}
							});
						}
						this.getData()
					},
					fail: function(e) {
						console.log('selectSql failed: ' + JSON.stringify(e));
					}
				});

				// setTimeout(() => {
				// 	this.inputValue = ''
				// }, 1)
			},

			del(e) {
				console.log(e.currentTarget.dataset.id);
				uni.showModal({
					title: '提示',
					content: '确定要删除吗',
					success: res => {
						if (res.confirm) {
							Vue.delete(this.lists, e.currentTarget.dataset.id)
							console.log(e.currentTarget.dataset.express);
							plus.sqlite.executeSql({
								name: 'express',
								// sql: `update express set status = 0 where express = ${e.currentTarget.dataset.express}`,
								sql: `delete from express where express = "${e.currentTarget.dataset.express}"`,
								success: (e) => {
									console.log('executeSql success!');
									this.getData()
								},
								fail: function(e) {
									console.log('executeSql failed: ' + JSON.stringify(e));
								}
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			getData() {
				plus.sqlite.selectSql({
					name: 'express',
					// sql: 'select * from express where  type = "box" and status = 1 LIMIT 2 ORDER BY scanTime DESC;',
					//1584264443480
					//1584288000000
					sql: 'select * from express where  type = "box" and status = 1 and scanTime > ' + this.nowTime +
						" ORDER BY scanTime DESC",
					//sql: 'select * from express where  type = "box" and status = 1 and scanTime > "${this.nowTime}"   ORDER BY scanTime DESC;',
					success: (data) => {
						console.log('selectSql success: ');
						this.lists = data
						console.log(data);
					},
					fail: function(e) {
						console.log('selectSql failed: ' + JSON.stringify(e));
					}
				});

			}
		},
		onShow() {
			this.getData()
			plus.device.getInfo({
				success: (e) => {
					this.uuid = e.uuid;
				},
				fail: function(e) {
					console.log('getDeviceInfo failed: ' + JSON.stringify(e));
				}
			});
			this.startScan();
		},
		onLoad: function(option) {
			this.initScan()
		},
		onHide: function() {
			this.stopScan();
		},
		onUnload: function() {
			/*页面退出时一定要卸载监听,否则下次进来时会重复，造成扫一次出2个以上的结果*/
			this.stopScan();
		},
	}
</script>

<style>

</style>
